//
//  InvestmentRecordVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/4/27.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "InvestmentRecordVC.h"

@interface InvestmentRecordVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _pageNo;
}

@property (weak, nonatomic) IBOutlet UILabel *leftTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *middleTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTypeLabel;

@property (weak, nonatomic) IBOutlet UITableView *mytableView;
@property (nonatomic, retain)NSMutableArray *totalArr;
@end

@implementation InvestmentRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mytableView.showsVerticalScrollIndicator = NO;
    self.navigationItem.title =  self.titleName;//@"投资记录";
    _pageNo = 1;
    
//    self.mytableView.allowsSelection = NO;
//    self.mytableView.userInteractionEnabled = YES;
    if (_ID) {
        [self startRefresh];

    }
//     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiCenter) name:@"investFinish" object:nil];
//    self.mytableView.contentOffset = CGPointMake(_mytableView.contentOffset.x, _mytableView.contentOffset.y+1);

    [self.view showHUDLoading];
    [self netWorking];

}
//- (void)notiCenter
//{
//    _pageNo = 1;
//    [self netWorking];
//}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    

}
#pragma mark- 刷新
- (void)startRefresh
{
    @weakify(self)
    self.mytableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self_weak_ netWorking];
    }];
    
    
}
#pragma tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totalArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InvestmentRecordCellID"];
    UILabel *nameLab = [cell viewWithTag:1001];
    UILabel *timeLab = [cell viewWithTag:1002];
    UILabel *moneyLab = [cell viewWithTag:1003];

    NSDictionary *dic = _totalArr[indexPath.row];
    NSString *name = [NSString stringJsonValue:dic[@"userAccount"]];
    if (name.length>6) {
        name = [name stringByReplacingCharactersInRange:NSMakeRange(3, name.length-7) withString:@"****"];
    }
    if (f_Device_w < 375) {
        nameLab.font =[UIFont systemFontOfSize:13*320/375];
//        return UIFont.systemFont(ofSize: f*320/375)
        
    }else{
        nameLab.font =[UIFont systemFontOfSize:13];
    }
//    nameLab.font =
    nameLab.text = name;
    NSString *time = [NSString stringJsonValue:dic[@"addTime"]];
    timeLab.text = [[time componentsSeparatedByString:@" "] firstObject];
    moneyLab.text = dic[@"amount"];
    
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        //end of loading
        dispatch_async(dispatch_get_main_queue(),^{
            //for example [activityIndicator stopAnimating];
            if (tableView.contentOffset.y==0) {
                tableView.contentOffset = CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y+1);
            }
        });
    }
    return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y==0) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y+1);
    }
}
- (void)setID:(NSString *)ID
{
    _ID = ID;
    
}
- (void)netWorking
{
    if (_ID) {
        [HYCNetService productsOrderListWithDict:@{@"id":_ID,@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"15"} onSuccess:^(NSDictionary *dictResult) {
            //
            [self.view hideHUDLoading:0];
            self.mytableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                
                [self netWorking];
            }];
            NSArray *lists = dictResult[@"userOrderList"];
            
            if (_pageNo==1) {
                [_totalArr removeAllObjects];
                [self.mytableView.mj_header endRefreshing];
            }else{
                [self.mytableView.mj_footer endRefreshing];
                
            }
            _pageNo++;
            self.mytableView.mj_footer.hidden = lists.count<15;
            self.leftTypeLabel.text = dictResult[@"userAccountLabel"];
            self.middleTypeLabel.text = dictResult[@"amountLabel"];
            self.rightTypeLabel.text = dictResult[@"addTimeLabel"];
            
            
            [self.totalArr addObjectsFromArray:lists];
            [self.mytableView reloadData];
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            //
            [self.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
            [self.mytableView.mj_header endRefreshing];
            [self.mytableView.mj_footer endRefreshing];
        }];
    }
    
}

- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
@end
